document.getElementById("random").onclick=function(){
	random()
}
	
function random(){
	$("#s-input > input").each(function(){
		this.value=""
	})
	$.ajax({
		"url":"/random/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"dataType":"jsonp",
		"success":function(data){
			if(!data.status){
				showErrorInfo(data.status)
				return false;
			}
			var content=data.content
			var i=0;
			$("#s-input > input").each(function(){
				this.value=content[i]
				i+=1
			})
			
		}
	})
}
function getList(){
	$.ajax({
		"url":"/serial_type_admin/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"dataType":"jsonp",
		"success":function(data){
			if(!data.status){
				showErrorInfo(data.content)
				return false;
			}
			var s1=document.getElementById("s-1")
			var s2=document.getElementById("s-2")
			var s3=document.getElementById("s-3")
			var content=data.content
			for(var i=0;i<content.length;i++){
				var option=document.createElement("option")
				option.textContent=content[i].name;
				option.setAttribute("value",content[i].id)
				s1.appendChild(option)
				option=option.cloneNode(true)
				s2.appendChild(option)
				option=option.cloneNode(true)
				s3.appendChild(option)
				
			}
		}
	})
}
function serialAdd(){
	var data={"serial":[]}
	var i=0
	$("#s-input input").each(function(){
		var v=this.value
		if(/^ *$/.test(v)){
			data.serial.push("")
		}
		else{
			if(v.toString().toUpperCase()==="O" || v.toString().toUpperCase()==="0" || v.toString().toUpperCase()==="1" ||  v.toString().toUpperCase()==="L"){
				showErrorInfo("第 "+ _i  + "个位置的格式不正确，不能包含0、O、l、L这四个字符!")
				return false;
			}
			data.serial.push(v)
		}
		i+=1
		
	})
	var status=null
	for(var i=0;i<data.serial.length;i++){
		if (data.serial[i]!==""){
			status=true
		}
	}
	if(status===null){
		showErrorInfo("序列号不能全为空！")
		return false;
	}
	data.type=$("#s-1").val()
	if(/^ *$/.test(data.type)){
		showErrorInfo("当前没有选择序列号！")
		return false;
	}
	data.valid_date=$("#validDate1").val()
	data.use_date=$("#useDate1").val()
	data.description=$("#description1").val()
	data=JSON.stringify(data)
	$.ajax({
		"url":"/create_single_serial/",
		"data":{"data":data},
		"type":"POST",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"success":function(data){
			var data=JSON.parse(data)
			if(!data.status){
				showErrorInfo(data.content)
				return false;
			}
			alert("操作成功！")
			location.reload()
		}
	})
}
function serialBatchAdd(){
	var data={"serial":[],"amount":document.getElementById("batchAmount").value}
	var i=0
	$("#b-input input").each(function(){
		var v=this.value
		if(/^ *$/.test(v)){
			data.serial.push("")
		}
		else{
			if(v.toString().toUpperCase()==="O" || v.toString().toUpperCase()==="0" || v.toString().toUpperCase()==="1" ||  v.toString().toUpperCase()==="L"){
				showErrorInfo("第 "+ _i  + "个位置的格式不正确，不能包含0、O、l、L这四个字符!")
				return false;
			}
			data.serial.push(v)
		}
		i+=1
		
	})
	var status=null
	for(var i=0;i<data.serial.length;i++){
		if (data.serial[i]!==""){
			status=true
		}
	}
	if(status===null){
		showErrorInfo("序列号不能全为空！")
		return false;
	}
	data.type=$("#s-2").val()
	if(/^ *$/.test(data.type)){
		showErrorInfo("当前没有选择序列号！")
		return false;
	}
	data.valid_date=$("#validDate2").val()
	data.use_date=$("#useDate2").val()
	data.description=$("#description2").val()
	data=JSON.stringify(data)
	$.ajax({
		"url":"/create_batch_serial/",
		"data":{"data":data},
		"type":"POST",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"success":function(data){
			var data=JSON.parse(data)
			if(!data.status){
				showErrorInfo(data.content)
				return false;
			}
			alert("操作成功！")
			location.reload()
		}
	})
}
function serialFileAdd(){
	if(document.getElementById("serial-file").value==""){
		showErrorInfo("请选择文件！")
		return false;
	}
	var file=$("#serial-file").prop("files")[0]
	var data=new FormData()
	data.append("file",file)
	var _data={}
	_data.type=$("#s-3").val()
	_data.valid_date=$("#validDate3").val()
	_data.use_date=$("#useDate3").val()
	_data.description=$("#description3").val()
	data.append("data",JSON.stringify(_data))
	jQuery.ajax({
		"url":"/create_file_serial/",
		"type":"POST",
		"data":data,
		"processData":false,
                "contentType": false,
		"error":errorAjax,
		"complete":stop_load_pic,
		"beforeSend":start_load_pic,
                "cache": false,
		"success":function(data){
			data=JSON.parse(data)
			if(!data.status){
				showErrorInfo(data.content)
				return false
			}
			else{
				alert("操作成功！")
				location.reload()
			}
		}
	})
}
document.getElementById("file-add").onclick=function(){
	serialFileAdd()
}
document.getElementById("b-add").onclick=function(){
	serialBatchAdd()
}
document.getElementById("s-add").onclick=function(){
	serialAdd()
}
document.getElementById("reset").onclick=function(){
	$("#s-input > input").each(function(){
		this.value=""
	})
}
getList()
